
//Gets the browser specific XmlHttpRequest Object
var globalFieldNo="";
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggestDistrict(fieldNo) {
		var distText = 'district_name'+fieldNo;
		var str = escape(document.getElementById(distText).value);
		//alert(str);
		globalFieldNo=fieldNo;
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById(distText).value);
		//alert('searchSuggestDistrict.php?search=' + str);
		searchReq.open("post", 'searchSuggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	var divText = 'search_suggest'+globalFieldNo;
	//alert(searchReq.responseText);
	//divText = 'search_suggest';
		
		
	if (searchReq.readyState == 4) {
		//alert(searchReq.responseText);
		//var divText = 'search_suggest';
		//alert(divText);
		var ss = document.getElementById(divText)
		ss.innerHTML = '';
		var str = searchReq.responseText.split("<br>");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);"';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);"';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			//alert(suggest);
			ss.innerHTML += suggest;
		}
	}
}

//Auto suggest for block
function searchSuggestBlock(fieldNo) {
		var blockText = 'block_name'+fieldNo;
		var str = escape(document.getElementById(blockText).value);
		var distText = 'district_name'+fieldNo;
		strDist = document.getElementById(distText).value;
		if(strDist==""){ alert("Please enter district name first");document.getElementById(distText).focus();return false ; }
		globalFieldNo=fieldNo;
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById(blockText).value);
		//alert('searchSuggestBlock.php?search=' + str+'&distId='+strDist);
		searchReq.open("post", 'searchSuggestBlock.php?search=' + str+'&distName='+strDist, true);
		searchReq.onreadystatechange = handleSearchSuggestBlock; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggestBlock() {
	var blockText = 'search_suggest_block'+globalFieldNo;
	//alert(searchReq.responseText);
	//divText = 'search_suggest';
		
		
	if (searchReq.readyState == 4) {
		//alert(searchReq.responseText);
		//var divText = 'search_suggest';
		//alert(divText);
		var ss = document.getElementById(blockText)
		ss.innerHTML = '';
		var str = searchReq.responseText.split("<br>");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);"';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearchBlock(this.innerHTML);"';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			//alert(suggest);
			ss.innerHTML += suggest;
		}
	}
}


//code for Panhayat
function searchSuggestPanchayat(fieldNo) {
		var panchayatText = 'panchayat_name'+fieldNo;
		var str = escape(document.getElementById(panchayatText).value);
		var distText = 'district_name'+fieldNo;
		var blockText = 'block_name'+fieldNo;
		var panchayatText = 'panchayat_name'+fieldNo;
		strDist = document.getElementById(distText).value;
		strBlock=document.getElementById(blockText).value;
		if(strDist==""){ alert("Please enter district name first");document.getElementById(distText).focus();return false ; }
		if(strBlock==""){ alert("Please enter block name first");document.getElementById(blockText).focus();return false ; }
		globalFieldNo=fieldNo;
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById(panchayatText).value);
		//alert('searchSuggestPanchayat.php?search=' + str+'&distName='+strDist);
		searchReq.open("post", 'searchSuggestPanchayat.php?search=' + str+'&distName='+strDist+'&blockName='+strBlock, true);
		searchReq.onreadystatechange = handleSearchSuggestPanchayat; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggestPanchayat() {
	var panchayatText = 'search_suggest_panchayat'+globalFieldNo;
	//alert(searchReq.responseText);
	//divText = 'search_suggest';
		
		
	if (searchReq.readyState == 4) {
		//alert(searchReq.responseText);
		//var divText = 'search_suggest';
		//alert(divText);
		var ss = document.getElementById(panchayatText)
		ss.innerHTML = '';
		var str = searchReq.responseText.split("<br>");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);"';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearchPanchayat(this.innerHTML);"';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			//alert(suggest);
			ss.innerHTML += suggest;
		}
	}
}

//code for Village
function searchSuggestVillage(fieldNo) {
		var villageText = 'village_name'+fieldNo;
		var str = escape(document.getElementById(villageText).value);
		var distText = 'district_name'+fieldNo;
		var blockText = 'block_name'+fieldNo;
		var panchayatText = 'panchayat_name'+fieldNo;
		var villageText='village_name'+fieldNo;
		strDist = document.getElementById(distText).value;
		strBlock=document.getElementById(blockText).value;
		strPanchayat=document.getElementById(panchayatText).value;
		if(strDist==""){ alert("Please enter district name first");document.getElementById(distText).focus();return false ; }
		if(strBlock==""){ alert("Please enter block name first");document.getElementById(blockText).focus();return false ; }
		if(strPanchayat==""){ alert("Please enter panchayat name first");document.getElementById(panchayatText).focus();return false ; }
		globalFieldNo=fieldNo;
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById(villageText).value);
		//alert('searchSuggestVillage.php?search=' + str+'&distId='+strDist);
		searchReq.open("post", 'searchSuggestVillage.php?search=' + str+'&distName='+strDist+'&blockName='+strBlock+'&panchayatName='+strPanchayat, true);
		searchReq.onreadystatechange = handleSearchSuggestVillage; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggestVillage() {
	var villageText = 'search_suggest_village'+globalFieldNo;
	//alert(searchReq.responseText);
	//divText = 'search_suggest';
		
		
	if (searchReq.readyState == 4) {
		//alert(searchReq.responseText);
		//var divText = 'search_suggest';
		//alert(divText);
		var ss = document.getElementById(villageText)
		ss.innerHTML = '';
		var str = searchReq.responseText.split("<br>");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);"';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearchVillage(this.innerHTML);"';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			//alert(suggest);
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
	document.getElementById('district_name'+globalFieldNo).value = value;
	document.getElementById('search_suggest'+globalFieldNo).innerHTML = '';
}
function setSearchBlock(value) {
	document.getElementById('block_name'+globalFieldNo).value = value;
	document.getElementById('search_suggest_block'+globalFieldNo).innerHTML = '';
}

function setSearchPanchayat(value) {
	document.getElementById('panchayat_name'+globalFieldNo).value = value;
	document.getElementById('search_suggest_panchayat'+globalFieldNo).innerHTML = '';
}

function setSearchVillage(value) {
	document.getElementById('village_name'+globalFieldNo).value = value;
	document.getElementById('search_suggest_village'+globalFieldNo).innerHTML = '';
}